import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import authAPI from '@/api/authAPI'
import departmentAPI from "@/api/system/departmentAPI";

const getDefaultState = () => {
  return {
    token: getToken(),
    username: '',
    nickName: '',
    phone: '',
    sex: 0,
    email: '',
    department: '',
    avatar: '',
    departMap: new Map()
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USERNAME: (state, username) => {
    state.username = username
  },
  SET_NICKNAME: (state, nickname) => {
    state.nickname = nickname
  },
  SET_PHONE: (state, phone) => {
    state.phone = phone
  },
  SET_SEX: (state, sex) => {
    state.sex = sex
  },
  SET_EMAIL: (state, email) => {
    state.email = email
  },
  SET_DEPARTMENT_ID: (state, departmentId) => {
    state.departmentId = departmentId
  },
  SET_AVATAR: (state, cover) => {
    state.avatar = cover
  },
  SET_DEPARTMENT_MAP: (state, departMap) => {
    state.departMap = departMap
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      authAPI.login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.tokenValue)
        setToken(data.tokenValue)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getCurrentUserInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      authAPI.getCurrentUserInfo().then(response => {
        const { data } = response
        if (!data) {
          return reject('验证失败, 请重新登录.')
        }
        const { username, nickname, phone, sex, email, departmentId, cover } = data.currentUser
        commit('SET_USERNAME', username)
        commit('SET_NICKNAME', nickname)
        commit('SET_PHONE', phone)
        commit('SET_SEX', sex)
        commit('SET_EMAIL', email)
        commit('SET_DEPARTMENT_ID', departmentId)
        commit('SET_AVATAR', cover)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get DepartmentInfo
  getDepartmentInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      departmentAPI.querySysDepartmentList().then(response => {
        const { data } = response
        if (!data || data.length === 0) {
          return reject('读取数据失败, 请重新登录.')
        }
        const departMap = new Map()
        data.forEach(depart => {
          departMap.set(depart.id, depart.name)
        })
        commit('SET_DEPARTMENT_MAP', departMap)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      authAPI.logout().then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

